package cn.qitom.auth.granter;

import cn.hutool.extra.spring.SpringUtil;
import cn.qitom.service.user.feign.IUserClient;
import lombok.SneakyThrows;

import java.util.HashMap;
import java.util.Map;

public class LegoGranterManager {

    private static IUserClient userClient;

   private static Map<String,Class<ILegoGranter>> router= new HashMap(){{
       put(PhoneGranter.ROUTE,PhoneGranter.class);
       put(CodeGranter.ROUTE,CodeGranter.class);
       put("",PhoneGranter.class);//default
       put(null,PhoneGranter.class);//default
   }};

    @SneakyThrows
    public static ILegoGranter route(GranterProp prop){

        if(userClient==null)userClient= SpringUtil.getBean(IUserClient.class);

        Class<ILegoGranter> granterClass = router.get(prop.getType());
        Class[] cArg = new Class[2];//按照顺序放入构造器参数，实现类需要声明构造器
        cArg[0] = GranterProp.class;
        cArg[1] = IUserClient.class;
        ILegoGranter iLegoGranter = granterClass.getDeclaredConstructor(cArg).newInstance(prop,userClient);
        return iLegoGranter;
    }
}
